package com.abing.chatRoom.controller;

import com.abing.chatRoom.common.ErrorCode;
import com.abing.chatRoom.common.ResultUtils;
import com.abing.chatRoom.exception.BusinessException;
import com.abing.chatRoom.model.VO.UserVO;
import com.abing.chatRoom.model.domain.Friend;
import com.abing.chatRoom.service.FriendService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.abing.chatRoom.common.BaseResponse;
import com.abing.chatRoom.model.domain.User;
import com.abing.chatRoom.model.request.FriendAddRequest;
import com.abing.chatRoom.model.request.PageRequest;
import com.abing.chatRoom.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin(originPatterns = {"http://localhost"}, allowCredentials = "true", allowedHeaders = {"*"})
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Resource
    private UserService userService;
    @Resource
    private FriendService friendService;

    @GetMapping("/list/page")
    public BaseResponse<Page<Friend>>friendListByPage(PageRequest pageRequest, HttpServletRequest request){
        if(request==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Friend> page = new Page<Friend>(pageRequest.getPageNum(),pageRequest.getPageSize());
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",loginUser.getId());
        Page<Friend> resultPage =friendService.page(page,queryWrapper);
        return ResultUtils.success(resultPage);
    }
    @GetMapping("/list")
    public BaseResponse<List<UserVO>>friendList(HttpServletRequest request){
        if(request==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        List<UserVO> userVOS = friendService.friendList(loginUser);
        return ResultUtils.success(userVOS);
    }

    @PostMapping("/add")
    public BaseResponse<Boolean>friendAdd(@RequestBody FriendAddRequest friendAddRequest, HttpServletRequest request){
        if(request==null|| friendAddRequest ==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        friendService.friendAdd(friendAddRequest.getId(),request);
        return ResultUtils.success(true);
    }

    @GetMapping("/isFriend")
    public BaseResponse<Boolean>isFriend(Long id,HttpServletRequest request){
        if(request==null||id==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long userId = loginUser.getId();
        return ResultUtils.success(friendService.isFriend(id,userId));
    }
    @GetMapping("/delete")
    public BaseResponse<Boolean>friendDelete(Long id,HttpServletRequest request){
        if(request==null||id==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        friendService.friendDelete(id,request);
        return ResultUtils.success(true);
    }


}
